package com.cloudlead.fleetAction.action;

import com.cloudlead.common.lang.annotation.Label;
import com.cloudlead.common.lang.persistence.Criteria;
import com.cloudlead.common.lang.persistence.EasyuiGridData;
import com.cloudlead.common.lang.persistence.Filter;
import com.cloudlead.common.lang.persistence.Term;
import com.cloudlead.common.worker.action.impl.AbstractActionFactory;
import com.cloudlead.common.worker.annotation.Action;
import com.cloudlead.common.worker.annotation.Order;
import com.cloudlead.commonData.service.TbContainerInfoService;
import com.cloudlead.commonData.service.TbFleetOrderService;
import com.cloudlead.commonData.vo.TbContainerInfo;
import com.cloudlead.commonData.vo.TbFleetOrder;
import com.cloudlead.fleetAction.BusinessModules;
import com.cloudlead.fleetAction.service.ContainerSer;
import com.cloudlead.fleetAction.service.FleetOrderSer;
import com.cloudlead.fleetAction.service.impl.FleetOrderSerImpl;
import com.cloudlead.fleetService.FleetOrderService;
import com.cloudlead.fleetService.vo.ContainerVo;
import com.cloudlead.fleetService.vo.FleetOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by 20150817 on 2015/9/1.
 */
@Controller
public class FleetOrderActionFactory extends AbstractActionFactory implements FleetOrderService{
    public FleetOrderActionFactory() {
        super(BusinessModules.FleetOrderGL);
    }
    @Autowired
    FleetOrderSer fleetOrderSer;
    @Autowired
    ContainerSer containerSer;
    @Autowired
    TbContainerInfoService containerInfoService;
    @Autowired
    TbFleetOrderService fleetOrderService;
    @Autowired
    FleetOrderSerImpl flOrderService;

    @Action
    @Order(6)
    @Label("按订单列表派车")
    @Override
    public EasyuiGridData fleetOrderList( @Label("订单信息") FleetOrderVo vo, @Label("页数")int page,@Label("条数") int rows) {
        EasyuiGridData grdData=
                new EasyuiGridData(fleetOrderSer.findAllFleetOrder(vo, page, rows),
                        "id,quantity,orderType,createTime,doBoxDate,flag,isStart,orderState,price,bussinessCode,baseVoyag," +
                                "polName,delName,customerCo.companyShortName companyShortName,customerCo.id tbCustomerOrder," +
                                "baseVoya.vessel vessel," +
                                "baseVoya.voyage voyage," +
                                "baseVoya.arriveExpectedTime arriveExpectedTime," +
                                "baseVoya.arriveTime arriveTime," +
                                "baseVoya.startExpectedTime startExpectedTime," +
                                "baseVoya.startTime startTime," +
                                "tbCustomerOrder.id customerOrderId,tbCustomerOrder.destCompany.companyName customer1,tbCustomerOrder.baseCompany.companyName customer2," +
                                "fleetCompany.companyName companyName," +
                                "packagingAddress,removeAddress,cargoName,storeArea," +
                                "customerCo.id customerCode," +
                                "vesselVoyage," +
                                "customerCo.companyShortName customerName");
        FillContactInfo(grdData);
        return grdData;
    }


    @Action
    @Order(6)
    @Label("按箱信息列表派车")
    @Override
    public EasyuiGridData containerList(@Label("箱信息") ContainerVo cVo, @Label("页数")int page,@Label("条数") int rows) {
        return new EasyuiGridData(containerSer.findAllContainer(cVo, page, rows),
                    "id,fleetStatus,temp,dangerous,containerNo,containerSeal,trailerFlag,contactPerson," +
                            "contactTel,sendCarPepole,carNumber,fleetOrder.cargoName cargoName,fleetOrder.storeArea storeArea," +
                            "feedBack,minato,flag,volume,type,title," +
                            "fleetOrder.packagingAddress packagingAddress,fleetOrder.removeAddress removeAddress,suitcaseTime,intoTime,finishTime,outTime，shipStatus," +
                            "containerWeight,planDate,saveDate,fleetOrder.customerCo.companyShortName companyShortName,fleetOrder.orderType orderType," +
                            "tbCustomerOrder.id customerOrderId," +
                            "tbCustomerOrder.baseCompany.companyName shipCompanyName," +
                            "tbCustomerOrder.destCompany.companyName destCompanyName," +
                            "tbCustomerOrder.commodity commodity," +
                            "tbCustomerOrder.orderUser orderUser," +
                            "tbCustomerOrder.ordertime ordertime," +
                            "tbCustomerOrder.refrigeratingTemperature refrigeratingTemperature," +
                            "tbCustomerOrder.quantity quantity," +
                            "tbCustomerOrder.ventilation ventilation," +
                            "comments," +
                            "trailerDriver.name driverName," +
                            "fleetOrder.isStart isStart," +
                            "driver.name driverName," +
                            "fleetOrder.polName polName," +
                            "fleetOrder.delName delName," +
                            "fleetOrder.bussinessCode bussinessCode," +
                            "fleetOrder.fleetCompany.companyName fleetCompanyName," +
                            "car.id carId," +
                            "car.licencePlate carLicencePlate," +
                            "fleetStatus ," +
                            "car.licencePlate licencePlate,"+
                            "fleetOrder.vesselVoyage vesselVoyage," +
                            "fleetOrder.baseVoya.vessel vessel," +
                            "fleetOrder.baseVoya.voyage voyage," +
                            "fleetOrder.baseVoya.arriveExpectedTime arriveExpectedTime," +
                            "fleetOrder.baseVoya.arriveTime arriveTime," +
                            "fleetOrder.baseVoya.startExpectedTime startExpectedTime," +
                            "fleetOrder.baseVoya.startTime startTime," +
                            "trailerCar.licencePlate tcLicencePlate" +
                            "trailerDriver.name tdName");

    }

    @Action
    @Order(6)
    @Label("录入车队订单")
    @Override
    public String createOrder(FleetOrderVo vo) {
        return fleetOrderSer.createOrder(vo);
    }

    @Action
    @Order(6)
    @Label("修改车队订单")
    @Override
    public void updateOrder(FleetOrderVo vo) {
        fleetOrderSer.updateOrder(vo);
    }

    @Action
    @Order(6)
    @Label("删除车队订单")
    @Override
    public void deleteOrder(String id) {
        fleetOrderSer.deleteOrder(id);
    }

    @Action
    @Order(6)
    @Label("增加自录箱")
    @Override
    public String addOrderCon(ContainerVo vo) {
        containerSer.addOrderCon(vo);
        return UpdateFleetCtnQty(vo.getFleetorderid());
    }

    @Action
    @Order(6)
    @Label("修改自录箱")
    @Override
    public String updateOrderCon(ContainerVo vo) {
        containerSer.updateOrderCon(vo);
        return UpdateFleetCtnQty(vo.getFleetorderid());
    }

    @Action
    @Order(6)
    @Label("删除自录箱")
    @Override
    public String deleteOrderCon(String id) {
        TbContainerInfo tci = containerInfoService.view(id);
        containerSer.deleteOrderCon(id);
        return UpdateFleetCtnQty(tci.getFleetOrder().getId());
    }


    private void FillContactInfo(EasyuiGridData grdData){
        for (Object obj:grdData.getRows()){
            HashMap map=(HashMap)obj;
            List<Term> tr = new ArrayList<Term>();
            tr.add(new Term("fleet_order_id",map.get("id")));
            Filter filter = new Filter(new Criteria(tr));
            TbContainerInfo ctn=containerInfoService.findOne(filter);
            if (ctn!=null){
                map.put("contactTel",ctn.getContactTel());
                map.put("contactPerson",ctn.getContactPerson());
                map.put("planDate",ctn.getPlanDate());
                map.put("comments",ctn.getComments());
            }
        }
    }

    private String UpdateFleetCtnQty(String fleetOrderId){
        TbFleetOrder fo=fleetOrderService.view(fleetOrderId);
        if (fo==null) return "";
        List<Term> tr = new ArrayList<Term>();
        tr.add(new Term("fleet_order_id",fleetOrderId));
        tr.add(new Term("flag",0));
        Filter filter = new Filter(new Criteria(tr));
        List<TbContainerInfo> list=containerInfoService.list(filter,null);
        HashMap<String,Integer> map=new HashMap<>();
        for (TbContainerInfo ctn:list){
            String ctnType=ctn.getType();
            if (!map.containsKey(ctnType)) map.put(ctnType,0);
            map.put(ctnType,map.get(ctnType)+1);
        }
        String ctnQty="";
        for (String cType:map.keySet()){
            ctnQty+=" "+map.get(cType)+"X"+cType;
        }
        fo.setQuantity(ctnQty.trim());
        fleetOrderService.update(fo);
        return ctnQty.trim();
    }
    @Action
    @Label("订单审核通过")
    public void auditOk(FleetOrderVo vo){
        flOrderService.auditOk(vo);
    }
    @Action
    @Label("订单审核拒绝")
    public void auditNo(FleetOrderVo vo){
        flOrderService.auditNo(vo);
    }
}
